﻿using System;
using System.Management;
using ClusterAwareProperties;
using ClusterAwareProperties.Properties;

namespace ClusterAwareProperties
{
    /// <summary>
    /// Wrapper class Group ManagementObject
    /// </summary>
    public class GroupProperty : IDisposable
    {
        #region private property

        private readonly ManagementObject _classInstance;

        #endregion

        #region ctor

        /// <summary>
        /// constuctor
        /// </summary>
        private GroupProperty()
        {
            //GroupType = GroupType.Unknown;
            State = StateGroupResources.StateUnknown;
            Status = StatusResource.Unknown;
            Characteristics = CharacteristicsGroup.Unknown;
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="queryobj">ResourceGroup ManagementObject instance</param>
        public GroupProperty(ManagementObject queryobj)
            : this()
        {
            var path = queryobj.ToString();
            if (path.IndexOf("\\root\\MSCluster:MSCluster_ResourceGroup") == -1)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_TypeClassInvalid, path,"\\root\\MSCluster:MSCluster_ResourceGroup"));
            }
            _classInstance = queryobj;
            this.Convert(queryobj);
        }

        #endregion

        #region public properties

        /// <summary>
        /// Return instance of ResourceGroup ManagementObject
        /// </summary>
        public ManagementObject Instance
        {
            get { return _classInstance; }
        }


        /// <summary>
        /// Provides a short textual description of the group.
        /// </summary>
        public string Caption { get; internal set; }

        /// <summary>
        /// Provides comments about the group.
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// Indicates when the group was installed. The lack of a value does not indicate that the group is not installed.
        /// </summary>
        public DateTime? InstallDate { get; internal set; }

        /// <summary>
        /// Specifies the name of the group.
        /// </summary>
        public string Name { get; internal set; }

        /// <summary>
        /// The current status of the resource group.
        /// </summary>
        public StatusResource Status { get; set; }

        /// <summary>
        /// Provides access to the flags set for the group. For a list of possible characteristics, see CLUSCTL_GROUP_GET_FLAGS.
        /// </summary>
        public UInt32 Flags { get; set; }

        /// <summary>
        /// Provides access to the characteristics set for the group
        /// </summary>
        public CharacteristicsGroup Characteristics { get; set; }

        /// <summary>
        /// The current state of the resource group.
        /// </summary>
        public StateGroupResources State { get; set; }

        /// <summary>
        /// property specifies whether the group should automatically be failed back to the node identified as its preferred owner when that node comes back online following a failover.
        /// </summary>
        public UInt32 AutoFailbackType { get; set; }

        /// <summary>
        /// The FailbackWindowEnd property provides the latest time that the group can be moved back to the node identified as its preferred node.
        /// </summary>
        ///<remarks>
        /// Qualifiers: Min (-1) , Max (23) , Units ("hours") 
        /// </remarks>
        public UInt32 FailbackWindowEnd { get; set; }

        /// <summary>
        /// The FailbackWindowStart property provides the earliest time (that is, local time as kept by the cluster) that the group can be moved back to the node identified as its preferred node.
        /// </summary>
        ///<remarks>
        /// Qualifiers: Min (-1) , Max (23) , Units ("hours") 
        /// </remarks>
        public UInt32 FailbackWindowStart { get; set; }

        /// <summary>
        /// The FailoverPeriod property specifies a number of hours during which a maximum number of failover attempts, specified by the FailoverThreshold property, can occur.
        /// </summary>
        /// <remarks>
        /// Qualifiers: Min (1) , Max (1193) , Units ("hours") 
        /// </remarks>
        public UInt32 FailoverPeriod { get; set; }

        /// <summary>
        /// The FailoverThreshold property specifies the maximum number of failover attempts.
        /// </summary>
        public UInt32 FailoverThreshold { get; set; }

        /// <summary>
        /// identifies groups that should not be hosted on the same cluster node
        /// </summary>
        /// <remarks>Windows Server 2003:  This characteristic is not supported.</remarks>
        public string[] AntiAffinityClassNames { get; set; }

        ///<summary>
        /// The PersistentState property specifies whether a group should be left offline or brought online when the Cluster service starts.
        ///</summary>
        public bool PersistentState { get; set; }

        /// <summary>
        /// Provides access to the group's MSCluster_Property class.
        /// </summary>
        /// <remarks>
        /// This property is not supported before Windows Server 2008.
        /// </remarks>
        public PrivatePropertiesData PrivateProperties { get; set; }

        /// <summary>
        /// Provides the current internal state of the resource group.
        /// </summary>
        /// <remarks>
        /// This property is not supported before Windows Server 2008.
        /// </remarks>
        public string InternalState { get; internal set; }

        // <summary>
        // Priority value of the resource group
        // </summary>
        // <remarks>
        // Qualifiers: Min (0) , Max (512) 
        // This property is not supported before Windows Server 2008 R2.
        // </remarks>
        //public UInt32? Priority { get; set; }

        // <summary>
        // Number of the last node the resource group was activated on or explicitly moved to.
        // </summary>
        // <remarks>This property is not supported before Windows Server 2008 R2.</remarks>
        //public UInt32? DefaultOwner { get; set; }

        // <summary>
        // The Type of the resource group.
        // </summary>
        // <remarks>This property is not supported before Windows Server 2008 R2.</remarks>
        //public GroupType GroupType { get; internal set; }

        #endregion

        #region IDisposable Members

        /// <summary>
        /// Dispose Class
        /// </summary>
        public void Dispose()
        {
            if (Instance != null)
            {
                Instance.Dispose();
            }
        }

        #endregion
    }

}
